package com.linkhub.oms.admin.modules.oss.service.impl;

import cn.hutool.core.util.IdUtil;
import com.linkhub.oms.admin.common.enums.OssObjectModuleEnum;
import com.linkhub.oms.admin.common.exception.ExceptionFactory;
import com.linkhub.oms.admin.modules.oss.domain.param.PutObjectParam;
import com.linkhub.oms.admin.modules.oss.domain.vo.PutObjectResultVO;
import com.linkhub.oms.admin.modules.oss.service.AliyunOSSBizService;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.common.enums.IEnumConvert;
import com.linkhub.oms.common.util.QtBeanUtil;
import com.linkhub.oms.component.aliyunoss.dto.PutObjectResultDTO;
import com.linkhub.oms.component.aliyunoss.service.OSSApiService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Objects;

/**
 * @Description: AliyunOSSBizService
 * @Author: echo
 * @Date: 2024/5/9 20:45
 * @Version: 1.0
 */
@Slf4j
@Service
public class AliyunOSSBizServiceImpl implements AliyunOSSBizService {
    @Resource
    private OSSApiService ossApiService;

    @SneakyThrows
    @Override
    public PutObjectResultVO upload( PutObjectParam putObjectParam ) {

        Integer moduleType = putObjectParam.getModuleType();
        OssObjectModuleEnum module = IEnumConvert.valueOf(moduleType, OssObjectModuleEnum.class);
        if (Objects.isNull(module)) {
            throw ExceptionFactory.getI18nBizException("validation.oss.moduleType.range");
        }
        String userId = putObjectParam.getUserId();
        MultipartFile object = putObjectParam.getObject();
        String originalFilename = object.getOriginalFilename();
        String objectName = getObjectName(userId, module.getPath(), originalFilename);
        PutObjectResultDTO resultDTO = ossApiService.putObject(objectName, object.getInputStream());

        PutObjectResultVO vo = new PutObjectResultVO();
        QtBeanUtil.copyProperties(resultDTO, vo);
        vo.setOriginalFilename(originalFilename);

        return vo;

    }

    private String getObjectName( String userId, String modulePath, String fileName ) {
        if (StringUtils.isEmpty(userId)) {
            // 称重机器没有用户ID
            return String.format("%s/%s/%s", modulePath, IdUtil.fastSimpleUUID(), fileName);
        }
        return String.format("%s/%s/%s/%s", modulePath, userId, IdUtil.fastSimpleUUID(), fileName);
    }

    @SneakyThrows
    @Override
    public PutObjectResultVO upload( OssObjectModuleEnum module, InputStream in, String fileName ) {

        String objectName = getObjectName(OssObjectModuleEnum.WEIGHING_MACHINE_IMAGE.equals(module) ? null : LhSessionContext.getUserId(),
                module.getPath(), fileName);
        PutObjectResultDTO resultDTO = ossApiService.putObject(objectName, in);

        PutObjectResultVO vo = new PutObjectResultVO();
        QtBeanUtil.copyProperties(resultDTO, vo);
        vo.setOriginalFilename(fileName);
        return vo;
    }
}
